package com.jdb.social.controller;


import com.guns21.result.domain.PageResult;
import com.guns21.result.domain.Result;
import com.guns21.web.bind.annotation.CurrentUser;
import com.guns21.web.bind.annotation.RequireUuid;
import com.guns21.web.constant.SpringConstant;
import com.guns21.web.entity.UserInfo;
import com.jdb.social.api.dto.FavoriteDTO;
import com.jdb.social.provider.service.FavoriteCommandService;
import com.jdb.social.provider.service.FavoriteQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by jliu on 2016/10/26.
 */
@RestController
@RequestMapping("/api/social/v1")
public class SocialController {
    private static Logger logger = LoggerFactory.getLogger(SocialController.class);
    @Autowired
    private FavoriteCommandService favoriteCommandService;
    @Autowired
    private FavoriteQueryService favoriteQueryService;

    /**
     * 收藏视频
     *
     * @param referenceId
     * @param favorite    收藏还是取消收藏
     * @param user
     * @return
     */
    @PostMapping(value = "/favorite")
    public Result<FavoriteDTO> favorite(@RequestParam String referenceId,
                                        @RequestParam Byte type,
                                        @RequestParam(defaultValue = "true", required = false) Boolean favorite,
                                        @CurrentUser UserInfo user) {

        return favoriteCommandService.createFavorite(user.getId(), referenceId, type, favorite);
    }

    /**
     * 收藏列表
     *
     * @param time
     * @param forward
     * @param count
     * @param userId
     * @return
     */
    @GetMapping("/favorite/list")
    public PageResult<FavoriteDTO> favorites(@RequestParam(required = false) Date time,
                                             @RequestParam(defaultValue = "true", required = false) Boolean forward,
                                             @RequestParam(defaultValue = "20", required = false) Integer count,
                                             @RequireUuid(required = false) String userId) {
        return favoriteQueryService.listAssociations(userId, time, false, count);
    }

    /**
     * @param time
     * @param forward
     * @param count
     * @param userId
     * @return
     */
    @GetMapping("/favorite/ids")
    public PageResult<String> listId(@RequestParam(required = false) Date time,
                                     @RequestParam(defaultValue = "true", required = false) Boolean forward,
                                     @RequestParam(defaultValue = "20", required = false) Integer count,
                                     @RequireUuid(required = false) String userId) {

        return favoriteQueryService.listAssociationIds(userId, time, false, count);
    }

    @GetMapping("/favorite/{id:" + SpringConstant.UUID_REGEX_32 + "}")
    public Result favoriteDetail(@PathVariable(required = false) String id) {
        return Result.success(favoriteQueryService.get(id));
    }

}
